<?php

/*
 *
 */

class Model_InfoBase_Courts extends Model_BaseModel
{
    use Lib_BaseTraits;
    /**
     * 缓存前缀
     *
     * @var unknown
     */
    private static $tag = 'Model_InfoBase_Courts';
    /**
     * Undocumented variable
     *
     * @var string
     */
    private $table = "courts";

    public function __construct()
    {
        // 选择连接的数据库
        parent::_init('mediation');
    }

    /**添加
     * @param $insertdata
     * @return mixed
     */
    public function insert($insertdata)
    {
        $data = array();
        $sql = 'INSERT INTO `courts`(';
        $sql .= '`' . implode('`,`', array_keys($insertdata)) . '`';
        $sql .= ')VALUES(:' . implode(',:', array_keys($insertdata)) . ')';
        foreach ($insertdata as $key => $value) {
            $data[':' . $key] = $value;
        }
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->lastInsertId();
        $this->dao->clearTag(self::$tag);
        return $res;
    }

    /**
     * 修改数据
     */
    public function update($update, $id)
    {
        $data = [];
        $sql = "UPDATE `courts` SET ";
        $sqlarr = array();
        foreach ($update as $key => $value) {
            array_push($sqlarr, "`{$key}`=:$key");
            $data [":$key"] = $value;
        }
        $sql .= implode(',', $sqlarr);
        $sql .= " WHERE id=:id";
        $data[':id'] = $id;
        $res = $this->dao->conn(false)->noCache()->preparedSql($sql, $data)->affectedCount();
        $this->dao->clearTag(self::$tag);
        $this->dao->clearTag('Model_Mediate_Agent');
        $this->dao->clearTag('Model_InfoBase_PlaintiffTemplate');
        return $res;
    }

    /**
     * 获取律所列表
     */
    public function getList($start = 0, $limit = 0)
    {
        $sql = "SELECT * FROM {$this->table} ";
        $data = array();
        if ($limit) {
            $sql .= " ORDER BY id DESC LIMIT :start, :limit";
            $data[':start'] = $start;
            $data[':limit'] = intval($limit);
        }
        return $this->dao->conn()->setTag(self::$tag)->preparedSql($sql, $data)->fetchAll();
    }

    /**
     * 获取律所列表
     */
    public function getCount()
    {
        $sql = "SELECT COUNT(*) AS total FROM {$this->table} ";
        $data = array();
        return $this->dao->conn()->setTag(self::$tag)->preparedSql($sql, $data)->fetchOne();
    }
}
